package gui.reports.removed;

import gui.common.*;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Observer;
import java.util.Observable;

import reports.builders.HTMLBuilder;
import reports.builders.IBuilder;
import reports.builders.PDFBuilder;
import reports.directors.RemovedItemsReport;

import coreModel.HIT;

/**
 * Controller class for the removed items report view.
 */
public class RemovedReportController extends Controller implements
IRemovedReportController {

	/**
	 * Constructor.
	 * 
	 * @param view Reference to the removed items report view
	 */
	public RemovedReportController(IView view) {
		super(view);

		construct();

	}

	//
	// Controller overrides
	//

	/**
	 * Returns a reference to the view for this controller.
	 * 
	 * {@pre None}
	 * 
	 * {@post Returns a reference to the view for this controller.}
	 */
	@Override
	protected IRemovedReportView getView() {
		return (IRemovedReportView) super.getView();
	}

	/**
	 * Sets the enable/disable state of all components in the controller's view.
	 * A component should be enabled only if the user is currently
	 * allowed to interact with that component.
	 * 
	 * {@pre None}
	 * 
	 * {@post The enable/disable state of all components in the controller's view
	 * have been set appropriately.}
	 */
	@Override
	protected void enableComponents()
	{
		if(HIT.getInstance().getItemHistory().isThereALastTimeRan())
		{
			getView().enableSinceDate(true);
		}
		else
		{
			getView().enableSinceDate(true);
			getView().enableSinceLast(false);
		}

		if(getView().getSinceDate())
		{

			getView().setSinceLast(false);
			getView().enableSinceDateValue(true);
			getView().enableSinceDate(true);

		}
		if(getView().getSinceLast())
		{

			getView().setSinceLast(true);
			getView().enableSinceDateValue(false);
		}

	}

	/**
	 * Loads data into the controller's view.
	 * 
	 *  {@pre None}
	 *  
	 *  {@post The controller has loaded data into its view}
	 */
	@Override
	protected void loadValues()
	{
		//report has been ran in the past
		if(HIT.getInstance().getItemHistory().isThereALastTimeRan())
		{
			getView().setSinceDate(false);
			getView().setSinceLast(true);
			getView().setSinceLastValue(HIT.getInstance().getItemHistory().getlastTimeRan());
		}
		else
		{
			getView().setSinceDate(true);
			getView().setSinceDateValue(new Date());
			getView().enableSinceLast(false);
		}

	}

	//
	// IExpiredReportController overrides
	//

	/**
	 * This method is called when any of the fields in the
	 * removed items report view is changed by the user.
	 */
	@Override
	public void valuesChanged()
	{
		enableComponents();
	}

	/**
	 * This method is called when the user clicks the "OK"
	 * button in the removed items report view.
	 */
	@Override
	public void display() 
	{
		IBuilder builder;
		Date date;
		if(getView().getFormat()==FileFormat.HTML)
		{
			builder = new HTMLBuilder("Removed Item Report");
		}
		else
		{
			builder = new PDFBuilder("Removed Item Report");
		}

		//resolving date
		if(getView().getSinceDate())
		{
			date=getView().getSinceDateValue();
		}
		//since last
		else
		{
			date = HIT.getInstance().getItemHistory().getlastTimeRan();
		}

		RemovedItemsReport reporter= new RemovedItemsReport(date);
		reporter.build(builder);
		String file=builder.getFilePath();



		try
		{
			java.awt.Desktop.getDesktop().open(new File(file));
		}
		catch (IOException e)
		{
			getView().displayErrorMessage("Could not open pdf file to display report");
			e.printStackTrace();
		}

		HIT.getInstance().getItemHistory().setLastReportDate(new Date());
	}



}

